set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR aarch64)

set(HOME $ENV{HOME})
set(TOOLCHAIN_PATH "${HOME}/Downloads/gcc-arm-11.2-2022.02-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu")

if(DEFINED ENV{TOOLCHAIN_PATH})
    set(TOOLCHAIN_PATH $ENV{TOOLCHAIN_PATH})
endif()

if(DEFINED ENV{CMAKE_C_COMPILER})
    set(CMAKE_C_COMPILER $ENV{CMAKE_C_COMPILER})
endif()

if(DEFINED ENV{CMAKE_CXX_COMPILER})
    set(CMAKE_CXX_COMPILER $ENV{CMAKE_CXX_COMPILER})
endif()

if(DEFINED ENV{OH_DEVICE_BIN_PATH})
    set(CMAKE_DEST_DIR $ENV{OH_DEVICE_BIN_PATH})
else()
    set(CMAKE_DEST_DIR "/data/oh-llama.cpp")
endif()

if(DEFINED ENV{OH_DEVICE_LIB_PATH})
    set(CMAKE_LIB_PATH $ENV{OH_DEVICE_LIB_PATH})
else()
    set(CMAKE_LIB_PATH "/system/lib64_glibc")
endif()

if(NOT DEFINED CMAKE_C_COMPILER)
    set(CMAKE_C_COMPILER ${TOOLCHAIN_PATH}-gcc)
endif()

if(NOT DEFINED CMAKE_CXX_COMPILER)
    set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PATH}-g++)
endif()

set(CMAKE_EXE_LINKER_FLAGS "-Wl,-rpath-link,${CMAKE_CURRENT_LIST_DIR}/lib")
set(CMAKE_SHARED_LINKER_FLAGS "-Wl,-rpath-link,${CMAKE_CURRENT_LIST_DIR}/lib")


SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

# Set runtime RPATH for target device
set(CMAKE_INSTALL_RPATH "${CMAKE_DEST_DIR}/build/bin:${CMAKE_LIB_PATH}:/system/lib64")
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)

set(CMAKE_C_FLAGS "-O0 -g -pipe -march=armv8-a+simd")
set(CMAKE_CXX_FLAGS "-O0 -g -pipe -march=armv8-a+simd")